#region License

// Copyright (c) 2008, BXO Solutions AB
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, 
//       this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright 
//       notice, this list of conditions and the following disclaimer in the 
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the BXO Solutions AB nor the names of its 
//       contributors may be used to endorse or promote products derived from 
//       this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#endregion

using System;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace Bxo.TestingPortal.Entities.Tests {
    [TestFixture]
    public class ProjectFixture {
        [Test]
        [ExpectedException(typeof (InvalidOperationException))]
        public void CategoryAdding_CategoryBelongsToAnotherProject_Throws() {
            var project1 = new Project("proj1");
            var project2 = new Project("proj2");
            var category = new Category("cat");

            project1.Categories.Add(category);
            project2.Categories.Add(category);
        }

        [Test]
        [ExpectedException(typeof (InvalidOperationException))]
        public void CategoryAdding_CategoryBelongsToSameProject_Throws() {
            var testee = new Project("proj1");
            var category = new Category("cat");

            testee.Categories.Add(category);
            testee.Categories.Add(category);
        }

        [Test]
        [ExpectedException(typeof (ArgumentNullException))]
        public void CategoryAdding_CategoryNull_Throws() {
            var testee = new Project("proj1");
            testee.Categories.Add(null);
        }

        [Test]
        public void CategoryAdding_Works() {
            var project = new Project("a_project");
            var category = new Category("a_category");
            project.Categories.Add(category);

            Assert.That(project.Categories, Has.Member(category), "Category has not been added.");
            Assert.That(category.Project, Is.SameAs(project), "Category project has not been set.");
        }

        [Test]
        public void CategoryRemoving_CategoryHasNoProjects_Succeds() {
            var project = new Project("a_project");
            var testee = new Category("the_category");
            project.Categories.Add(testee);

            project.Categories.Remove(testee);

            Assert.That(testee.Project, Is.Null, "Category still belongs to the project.");
            Assert.That(project.Categories, Has.No.Member(testee), "Project still contains the category.");
        }

        [Test]
        [ExpectedException(typeof (InvalidOperationException))]
        public void CategoryRemoving_HasProjects_Throws() {
            var project = new Project("a_project");
            var scenario = new Scenario(new ScenarioVersion("some_scenario", string.Empty));
            project.Scenarios.Add(scenario);
            var testee = new Category("the_category");
            project.Categories.Add(testee);

            // In this test the category contains a project.
            testee.Scenarios.Add(scenario);

            project.Categories.Remove(testee);
        }

        [Test]
        [ExpectedException(typeof (ArgumentException))]
        public void CtorFailsOnEmptyName() {
            new Project(string.Empty);
        }

        [Test]
        [ExpectedException(typeof (ArgumentException))]
        public void CtorFailsOnNullName() {
            new Project(null);
        }

        [Test]
        public void CtorSetsProperties() {
            DateTime pre = DateTime.Now;

            var testee = new Project("the_project");

            DateTime post = DateTime.Now;

            Assert.That(testee.Name, Is.EqualTo("the_project"));
            Assert.That(testee.Scenarios, Is.Not.Null, "Problem with list of scenarios.");
            Assert.That(testee.Scenarios, Is.Empty, "Problem with list of scenarios.");
            Assert.That(testee.Categories, Is.Not.Null, "Problem with list of categories.");
            Assert.That(testee.Categories, Is.Empty, "Problem with list of categories.");
            Assert.That(testee.CreationDateTime, Is.GreaterThanOrEqualTo(pre) & Is.LessThanOrEqualTo(post), "Creation date time is not valid.");
        }

        [Test]
        public void IdSetterSuccess() {
            var testee = new Project("project_name") {
                    Id = 56
            };

            Assert.That(testee.Id, Is.EqualTo(56));
        }

        [Test]
        [ExpectedException(typeof (ArgumentException))]
        public void NameSetterFailsOnEmpty() {
            new Project("A") {
                    Name = string.Empty
            };
        }

        [Test]
        [ExpectedException(typeof (ArgumentException))]
        public void NameSetterFailsOnNull() {
            new Project("A") {
                    Name = null
            };
        }

        [Test]
        public void NameSetterOk() {
            var testee = new Project("A") {
                    Name = "B"
            };

            Assert.That(testee.Name, Is.EqualTo("B"));
        }

        [Test]
        public void ScenarioAdding_Works() {
            var testee = new Project("a_project");
            var scenario = new Scenario(new ScenarioVersion("a_summary", "an_instruction"));

            testee.Scenarios.Add(scenario);

            Assert.That(testee.Scenarios, Has.Count(1), "Scenario list count is invalid.");
            Assert.That(testee.Scenarios, Has.Member(scenario), "Scenario has not been added to the project.");
            Assert.That(scenario.Project, Is.SameAs(testee), "Project has not been set.");
        }

        [Test]
        [ExpectedException(typeof (NotSupportedException))]
        public void ScenarioClearing_Throws() {
            var testee = new Project("a_project");
            testee.Scenarios.Clear();
        }

        [Test]
        [ExpectedException(typeof (NotSupportedException))]
        public void ScenarioRemoving_Throws() {
            var testee = new Project("a_project");
            var scenario = new Scenario(new ScenarioVersion("a_summary", "an_instruction"));
            testee.Scenarios.Add(scenario);

            testee.Scenarios.Remove(scenario);
        }

        [Test]
        [ExpectedException(typeof (NotSupportedException))]
        public void ScenarioReplacing_Throws() {
            var testee = new Project("a_project");
            var scenario = new Scenario(new ScenarioVersion("a_summary", "an_instruction"));
            testee.Scenarios.Add(scenario);

            testee.Scenarios[0] = new Scenario(new ScenarioVersion("s2", "i2"));
        }

        [Test]
        public void ToStringReturnsName() {
            var testee = new Project("a_project");

            Assert.That(testee.ToString(), Is.SameAs("a_project"));
        }
    }
}